package com.example.fbi_text.Acitivity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.fbi_text.adapter.NewsAdapter;
import com.example.fbi_text.R;

import com.example.fbi_text.bean.News;
import com.example.fbi_text.database.DBHelper;
import com.example.fbi_text.views.HeaderView;

import java.util.List;

//  news的activity
public class MainNewsActivity extends AppCompatActivity   {
    private List<News> mNews;
    private HeaderView headerView;
    private NewsAdapter mNewsAdapter;
    private ListView mLVNews;
    Context mContext = this;

    // 定义回调接口
    public interface OnNewsUpdateListener {
        void onNewsUpdated();
    }
    private OnNewsUpdateListener newsUpdateListener;

    // 设置回调监听器
    public void setOnNewsUpdateListener(OnNewsUpdateListener listener) {
        this.newsUpdateListener = listener;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
//      显示我的文章
        headerView = findViewById(R.id.main_header);
        headerView.setSerchViewVisibility(View.GONE);
        headerView.setTitleVisibility(View.VISIBLE);
        headerView.setTitle("我的文章");
        mLVNews = findViewById(R.id.main_item01_listview);



        //      从库中提出数据显示
        mNews = DBHelper.getInstance(mContext).selectAllNews();
        mNewsAdapter = new NewsAdapter(mNews,mContext);
        mLVNews.setAdapter(mNewsAdapter);
        //   短按修改news_title
        mLVNews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                News news = (News) view.getTag(R.integer.news_tag);
                if (news != null) {
                    // 跳转到 ChangeTextActivity 并传递数据
                    Intent intent = new Intent(MainNewsActivity.this, ChangeTextActivity.class);
                    intent.putExtra("news_id", news.getNews_id());
                    intent.putExtra("news_title", news.getNews_title());
                    intent.putExtra("news_author", news.getNews_author());
                    intent.putExtra("news_time", news.getNews_time());
                    intent.putExtra("news_content", news.getNews_content());
                    startActivityForResult(intent, 1);
                }
            }
        });

        //长按列表的条目
        mLVNews.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                News news = (News) view.getTag(R.integer.news_tag);
                //弹框提示是否删除
                showDeleteDialog(news);
                return true;
            }

        });

        headerView.setmHeaderBackClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    // 处理返回结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            refreshNewsList();
            // 通知监听器数据已更新
            if (newsUpdateListener != null) {
                newsUpdateListener.onNewsUpdated();
            }
        }
    }
    //系统弹框
    private void showDeleteDialog(News news) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("删除");
        builder.setMessage("确定要删除吗？");
        builder.setIcon(R.mipmap.attention);
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                DBHelper.getInstance(mContext).deleteNewsById(news.getNews_id());
                showToast(news.getNews_content() + "信息被删除");
                refreshNewsList();
            }
        });
        Dialog dialog = builder.create();
        dialog.show();

    }


    //刷新列表：重新去数据库中查询数据，并将数据设置到Adapter中。
    public void refreshNewsList() {
        mNews = DBHelper.getInstance(mContext).selectAllNews();
        mNewsAdapter.updateData(mNews);
    }
    //基类中添加通用方法
    public void showToast(String message) {
        if(mContext != null && message != null) {
            Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
        }
    }
}
